﻿using Abp.Domain.Repositories;
using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using Camc.Quality.Bom;
using Camc.Quality.DataExporting.Excel.EpPlus;
using Camc.Quality.Dto;
using Camc.Quality.Storage;
using Camc.Quality.TrepanningManager.MatingManager;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Camc.Quality.TrepanningManager.BomNestingInterface
{
    public class BomMatingExportToExcel:EpPlusExcelExporterBase, IBomMatingExportToExcel
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;
        public BomMatingExportToExcel(
            ITimeZoneConverter timeZoneConverter,
            IAbpSession abpSession,
            ITempFileCacheManager tempFileCacheManager, IRepository<BomDesign, Guid> bomDesignrepository, IRepository<BomDesignDetail, Guid> bomDesignDetailrepository)
            : base(tempFileCacheManager)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
          
        }
        public string ExportToFile(List<MatingEditParentData>  matingDetaillists,int level)
        {
            
            return CreateExcelPackageLocal(
          "配套信息.xlsx",
          excelPackage =>
          {
              var sheet = excelPackage.Workbook.Worksheets.Add(L("配套信息"));
              sheet.OutLineApplyStyle = true;
             // var levelMax = _bomDesignDetailrepository.GetAll().Max(t => t.LevelNum);
              List<string> headerData = new List<string>();
              for (int i = 0; i <= level - 1; i++)
              {
                  headerData.Add("层级" + (i + 1));
                  if (i == 2)
                  {
                      headerData.Add(L("物料代码"));
                      headerData.Add(L("物料名称"));
                      headerData.Add(L("规格"));
                      headerData.Add(L("物料类别"));
                      headerData.Add(L("单位"));
                      headerData.Add(L("工艺用量"));
                      headerData.Add(L("芯级预配套数量"));
                      headerData.Add(L("可用预配套数量"));
                      headerData.Add(L("配套数量"));
                      headerData.Add(L("缺料数量"));
                      headerData.Add(L("出库数量"));
                      headerData.Add(L("配套状态"));
                      //headerData.Add(L("合格证号"));
                      //headerData.Add(L("放行单号"));
                      //headerData.Add(L("产品编码"));
              
                     // headerData.Add(L("单号数量"));
                      headerData.Add(L("详细"));
                      headerData.Add(L("配套人"));
                      headerData.Add(L("配套时间"));
                      headerData.Add(L("预配套人"));
                      headerData.Add(L("预配套时间"));
                      break;
                  }
              }
              AddHeaderDynamic(
                  sheet,
                 headerData
                  );

              AddDynamicObjects(
                  sheet, level, level, matingDetaillists,
                  _ =>_.LevelNum,
                  _ => _.Code,
                  _ => _.Name,
                  _ => _.Module,
                  _ => _.MaterialsTypeName,
                  _ => _.Unit,
                  _ => _.Number,
                  _ => _.PreSuiteStock,
                  _ => _.UsableMatingQuantityStock,
                  _ => _.SuiteStock,
                  _ => _.StarvingStock,
                  _ => _.DeliveryStock,
                  _ => _.MatingStatusName,
                  //_=>_.QualifiedNumber,
                  //_=>_.ReleaseOrder,
                  //_=>_.ProductNumber,
                  //_=>_.AllNumber,
                  _=>_.JoinText,
                  _ => _.CompletePeople,
                  _ => _.CompleteTime,
                  _ => _.PreSuiteName,
                   _ =>_.PreSuiteTime
                  );
              var creationTimeColumn15 = sheet.Column(19);
              creationTimeColumn15.Style.Numberformat.Format = "yyyy-mm-dd HH:mm:ss";
              var creationTimeColumn17 = sheet.Column(21);
              creationTimeColumn17.Style.Numberformat.Format = "yyyy-mm-dd HH:mm:ss";
              for (var i = 1; i <=21; i++)
              {
                  sheet.Column(i).AutoFit();
              }
          });
        }
    }
}
